﻿using AutoIt;
using System;
using System.Threading;
using System.Windows.Forms;
using YatuUI;

namespace Demo.PTGUI
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "BaizePTGUI", out runone);
            if (runone)
            {
                run.ReleaseMutex();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);


                //处理未捕获的异常  
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常  
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常  
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);


                Form frm = new FrmMain();
                if (!SoftReg.IsActive())
                    frm = new FrmRegister();

                Application.Run(frm);
            }
            else
            {
                MessageBox.Show("捷图2.0已经运行");
                Application.Exit();
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var error = e.ExceptionObject as Exception;
            Sys.Log($"CurrentDomain_UnhandledException:{error.Message} / {error.StackTrace}");
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var error = e.Exception;
            Sys.Log($"Application_ThreadException:{error.Message} / {error.StackTrace}");
        }
    }
}
